package com.victor9ac.tfm.social.connector.twitter;

import org.springframework.social.connect.Connection;
import org.springframework.social.oauth1.AuthorizedRequestToken;
import org.springframework.social.oauth1.OAuth1Operations;
import org.springframework.social.oauth1.OAuth1Parameters;
import org.springframework.social.oauth1.OAuthToken;
import org.springframework.social.twitter.api.Twitter;
import org.springframework.social.twitter.api.impl.TwitterTemplate;
import org.springframework.social.twitter.connect.TwitterConnectionFactory;
import org.springframework.stereotype.Component;


@Component
public class TwitterConnectionService {

	private static final String CONSUMER_SECRET = "6Sz1YjAPd3CkUDuHIG2SRHXOsLB77CmcbcZGyRJGPy0mcWLZzw";
	private static final String CONSUMER_KEY = "ZoM3RGH7x48G3KOeXsK3OTudF";

	public void twitterSingleConnect(){
		
		
		TwitterTemplate tt = new TwitterTemplate(CONSUMER_KEY, CONSUMER_SECRET);
		

		
	}
	
	public String getAutorizeUrlTwitter (){
		TwitterConnectionFactory connectionFactory = getTwitterConnectionFactory();
		
			OAuth1Operations oauthOperations = connectionFactory.getOAuthOperations();
			
			OAuthToken requestToken = getTwitterToken(oauthOperations);
			
			String authorizeUrl = oauthOperations.buildAuthorizeUrl( requestToken.getValue(), OAuth1Parameters.NONE );
			
			
			return authorizeUrl;
	
	}

	private TwitterConnectionFactory getTwitterConnectionFactory() {
		TwitterConnectionFactory connectionFactory = 
			    new TwitterConnectionFactory( CONSUMER_KEY, CONSUMER_SECRET );
		return connectionFactory;
	}
	
	private OAuthToken getTwitterToken (OAuth1Operations oauthOperations){
		
		OAuthToken requestToken = oauthOperations.fetchRequestToken( "https://levanzo.com", null);
		
		return requestToken;
	}
	
	public Connection<Twitter> getTwitterConnection (){
		
		TwitterConnectionFactory connectionFactory = getTwitterConnectionFactory();
		OAuth1Operations oauthOperations = connectionFactory.getOAuthOperations();
		
		Connection<Twitter> connection = connectionFactory.createConnection(getTwitterToken(oauthOperations));
		return connection;
		

	}
	
	public Twitter getTwitterApi (Connection<Twitter> connection){
		
		Twitter twitter = null;
		
		if (connection != null){
			twitter = connection.getApi();
		}
		return twitter;
		
	}
	
	
}
